package org.robot.component;

import java.util.HashMap;
import java.util.Map;

public class Attachment {
    private String url;
    private String type;
    private Attachment prev;
    private Map<String, Object> infoMap = new HashMap<>();
    private boolean isSeed = false;
    
    public Attachment(String url, String type) {
        this.url = url;
        this.type = type;
    }
    public Attachment(String url, Attachment prev) {
    	this.url = url;
    	this.prev = prev;
    }
    public Attachment(String url, String type, Attachment prev) {
    	this.url = url;
    	this.type = type;
    	this.prev = prev;
    }
    public Attachment(String url, String type, Attachment prev, Map<String, Object> infoMap) {
        this.url = url;
        this.type = type;
        this.prev = prev;
        this.infoMap = infoMap;
    }
    
    public boolean typeEquals(String type) {
        return getType().equals(type);
    }
    
    public Object get(String key) {
    	return getInfoMap().get(key);
    }
    
    public Object put(String key, Object value) {
    	return getInfoMap().put(key, value);
	}
    
    public Attachment prevAttachment(int prevNum) {
        Attachment prevAttachment = getPrev();
        for (int i = 1; i < prevNum && prevAttachment != null; i++) {
            prevAttachment = prevAttachment.getPrev();
        }
        return prevAttachment;
    }

    public Attachment prevAttachment() {
        return getPrev();
    }
    
    public String prevUrl() {
        return prevAttachment().getUrl();
    }

    /**
     * 返回前prevNum层的url，从1开始
     * @param prevNum
     * @return
     */
    public String prevUrl(int prevNum) {
        return prevAttachment(prevNum).getUrl();
    }
    
    
	public String getUrl() {
		return url;
	}
	public Attachment setUrl(String url) {
		this.url = url;
		return this;
	}
	public String getType() {
		return type;
	}
	public Attachment setType(String type) {
		this.type = type;
		return this;
	}
	public Attachment getPrev() {
		return prev;
	}
	public Attachment setPrev(Attachment prev) {
		this.prev = prev;
		return this;
	}
	public Map<String, Object> getInfoMap() {
		return infoMap;
	}
	public Attachment setInfoMap(Map<String, Object> infoMap) {
		this.infoMap = infoMap;
		return this;
	}
	public boolean isSeed() {
		return isSeed;
	}
	public Attachment setSeed(boolean isSeed) {
		this.isSeed = isSeed;
		return this;
	}
	@Override
	public String toString() {
		return "Attachment [url=" + url + ", type=" + type + ", isSeed=" + isSeed + ", infoMap=" + infoMap + ", prev="
				+ prev + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Attachment other = (Attachment) obj;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (url == null) {
			if (other.url != null)
				return false;
		} else if (!url.equals(other.url))
			return false;
		return true;
	}
	
    
    
    
    
}
